package com.springboot.controller.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.mapper.OrderFormMapper;
import com.springboot.mapper.PatientInfoFormMapper;
import com.springboot.mapper.UserLoginFormMapper;
import com.springboot.model.OrderForm;
import com.springboot.model.PatientMageInfo;
import com.springboot.model.UserLoginForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

@Controller
public class PatientInfoGetController {
    @Autowired
    OrderFormMapper orderFormMapper;
    @Autowired
    PatientInfoFormMapper patientInfoFormMapper;
    @Autowired
    UserLoginFormMapper userLoginFormMapper;

    @RequestMapping("/patientMage")
    @ResponseBody
    public PageInfo getAllPatient(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        //先去用户表中找到所有的患者用户，再去orderform表中找到预约过和没预约过的患者
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<PatientMageInfo> patientMageInfos = userLoginFormMapper.selectAllPatient();
//        ArrayList<PatientMageInfo> patientMageInfos = new ArrayList<>();
        System.out.println(patientMageInfos.size());
        if(patientMageInfos!=null&&patientMageInfos.size()>0){
            for (PatientMageInfo patientMageInfo : patientMageInfos) {
                ArrayList<OrderForm> patients = orderFormMapper.selectByPatientId(patientMageInfo.getUserId());
                if(patients.size()>0)patientMageInfo.setStatus("是");
                else patientMageInfo.setStatus("否");
            }
            return PageInfo.of(patientMageInfos);
        }else {
            return PageInfo.of(patientMageInfos);
        }
    }

    @RequestMapping("/patientMage/{queryString}")
    @ResponseBody
    public PageInfo getQueryStringPatient(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @PathVariable String queryString){
        //传过来的查询信息包括姓名或电话号
        String telRegex = "[1][35789]\\d{9}";
        String nameRegex = "[a-zA-Z\\u4e00-\\u9fa5 ]+";
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<PatientMageInfo> patientMageInfos = new ArrayList<>();
        if(queryString.matches(telRegex)){  //输入的查询是电话号
            PatientMageInfo patientMageInfo = new PatientMageInfo();
            UserLoginForm userLoginForm = userLoginFormMapper.selectByTel(queryString);
            patientMageInfo.setUserName(userLoginForm.getUserName());
            patientMageInfo.setUserSex(userLoginForm.getUserSex());
            patientMageInfo.setUserTel(userLoginForm.getUserTel());
            patientMageInfo.setLogoLink(userLoginForm.getLogoLink());
            ArrayList<OrderForm> patients = orderFormMapper.selectByPatientId(userLoginForm.getUserId());
            if(patients.size()>0)patientMageInfo.setStatus("是");
            else patientMageInfo.setStatus("否");
            patientMageInfos.add(patientMageInfo);
        }
        else if(queryString.matches(nameRegex)){  //输入的查询时姓名
            PatientMageInfo patientMageInfo = new PatientMageInfo();
            UserLoginForm userLoginForm = userLoginFormMapper.selectUserByName(queryString);
            patientMageInfo.setUserName(userLoginForm.getUserName());
            patientMageInfo.setUserSex(userLoginForm.getUserSex());
            patientMageInfo.setUserTel(userLoginForm.getUserTel());
            patientMageInfo.setLogoLink(userLoginForm.getLogoLink());
            ArrayList<OrderForm> patients = orderFormMapper.selectByPatientId(userLoginForm.getUserId());
            if(patients.size()>0)patientMageInfo.setStatus("是");
            else patientMageInfo.setStatus("否");
            patientMageInfos.add(patientMageInfo);
        }
        return PageInfo.of(patientMageInfos);
    }

    @RequestMapping("/bookedPatientMage")
    @ResponseBody
    public PageInfo getBookedPatient(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<PatientMageInfo> bookedPatients = userLoginFormMapper.getBookedPatient();
        for (PatientMageInfo bookedPatient : bookedPatients) {
            bookedPatient.setStatus("是");
        }
        return PageInfo.of(bookedPatients);
    }

    @RequestMapping("/unbookedPatientMage")
    @ResponseBody
    public PageInfo getUnbookedPatient(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<PatientMageInfo> patientMageInfos = userLoginFormMapper.getUnbookedPatient();
        for (PatientMageInfo patientMageInfo : patientMageInfos) {
            patientMageInfo.setStatus("否");
        }
        return PageInfo.of(patientMageInfos);
    }
}
